package com.dhcc.sdc.busi.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.core.framework.constant.state.LogSucceed;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.HttpContextUtil;
import com.dhcc.sdc.annotion.SdcBusiType;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.busi.dao.SdcBusiOperationLogMapper;
import com.dhcc.sdc.busi.service.ISdcBusiOperationLogService;
import com.dhcc.sdc.busi.entity.SdcBusiOperationLogEntity;
import org.springframework.transaction.annotation.Transactional;

/**
 * 单病种过程质控操作日志表数据服务层接口
 *
 * @ClassName: SdcBusiOperationLogServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2023-03-06 10:55:54
 */
@Service("sdcBusiOperationLogService")
public class SdcBusiOperationLogServiceImpl extends BaseServiceImpl<SdcBusiOperationLogMapper, SdcBusiOperationLogEntity> implements ISdcBusiOperationLogService {

	@Override
	public List<SdcBusiOperationLogEntity> page(Page<SdcBusiOperationLogEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdcBusiOperationLogEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public JSONObject logTypeMap() {
		JSONObject logTypeMap = new JSONObject();
		for (SdcBusiType value : SdcBusiType.values()) {
			logTypeMap.put(value.getCode(), value.getName());
		}
		return logTypeMap;
	}

    @Override
	@Transactional
    public boolean saveSdcBusiLog(Map<String, Object> params) {
		boolean flag = false;
		if (CommonUtil.isNotEmpty(params.get("actionType"))) {
			SdcBusiOperationLogEntity sdcBusiOperationLog = new SdcBusiOperationLogEntity();
			switch (params.get("actionType").toString()) {
				case "open":
					sdcBusiOperationLog.setLogType(SdcBusiType.OPEN_WINDOW.getCode());
					sdcBusiOperationLog.setLogName(SdcBusiType.OPEN_WINDOW.getName());
					break;
				case "treat":
					sdcBusiOperationLog.setLogType(SdcBusiType.TREAT_LOOK.getCode());
					sdcBusiOperationLog.setLogName(SdcBusiType.TREAT_LOOK.getName());
					break;
				case "qc":
					sdcBusiOperationLog.setLogType(SdcBusiType.QC_LOOK.getCode());
					sdcBusiOperationLog.setLogName(SdcBusiType.QC_LOOK.getName());
					break;
				case "ci":
					sdcBusiOperationLog.setLogType(SdcBusiType.CI_LOOK.getCode());
					sdcBusiOperationLog.setLogName(SdcBusiType.CI_LOOK.getName());
					break;
				default:
					break;
			}
			sdcBusiOperationLog.setParams(JSON.toJSONString(params));
			sdcBusiOperationLog.setUserId(CommonUtil.isEmpty(params.get("modifyUserId")) ? null : params.get("modifyUserId").toString());
			sdcBusiOperationLog.setUserName(params.get("modifyUserName").toString());
			sdcBusiOperationLog.setEpisodeId(params.get("episodeId").toString());
			sdcBusiOperationLog.setLocId(params.get("locId").toString());
			sdcBusiOperationLog.setQcentityId(Long.valueOf(params.get("qcentityId").toString()));
			sdcBusiOperationLog.setIpaddr(HttpContextUtil.getIp());
			sdcBusiOperationLog.setResult(LogSucceed.SUCCESS.getMessage());
			sdcBusiOperationLog.setCreateDate(new Date());
			flag = sdcBusiOperationLog.insert();
		}
		return flag;
	}
}